package com.csoa.community.services;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import com.csoa.community.net.ErrorMsg;
import com.csoa.community.requests.LinkmanRequest;
import com.csoa.community.requests.RiChengRequest;
import com.csoa.community.singletons.GlobalVars;
import com.csoa.community.utils.HttpUtils;
import com.csoa.community.utils.StringUtils;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class LocalService extends Service {
	List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();

	public class NoticeBinder extends Binder {
		public LocalService getService() {
			return LocalService.this;
		}
	}

	private NoticeBinder iBinder = new NoticeBinder();

	@Override
	public IBinder onBind(Intent intent) {
		new Thread() {
			public void run() {
				while (true) {

					try {
						
						// 待阅公文数
						if (GlobalVars.currentUser != null && !StringUtils.isEmpty(GlobalVars.currentUser.loginName)) {
							String unReadCount = "";
							// 未读公文数
							params.clear();
							params.add(new BasicNameValuePair("LogName",
									GlobalVars.currentUser.loginName));
							unReadCount = HttpUtils.getInst()
									.getStringByWcf("GetArchivesCount", params);
							if (!StringUtils.isEmpty(unReadCount)) {
								sendBroadcast("document", unReadCount);
							}

							if (GlobalVars.currentUser != null && !StringUtils.isEmpty(GlobalVars.currentUser.loginName)) {
								// 未读邮件数
								params.clear();
								params.add(new BasicNameValuePair("uid",
										GlobalVars.currentUser.loginName
										));
								unReadCount = HttpUtils.getInst()
										.getStringByWcf("GetUnseenMessagesCount", params,GlobalVars.serviceEmailURL,GlobalVars.soapEmailAction);
								if (!StringUtils.isEmpty(unReadCount)) {
									sendBroadcast("email", unReadCount);
								}
							}
						}

						// 已阅公文数
						// String haveReadCount =
						// HttpUtils.getInst().getStringByWcf("GetArchivesReadCount",
						// params);
						// if(!StringUtils.isEmpty(haveReadCount)) {
						// sendBroadcast("haveReadCount",unReadCount);
						// }

					} catch (ErrorMsg e) {
						e.printStackTrace();
					}
					try {
						sleep(1000 * 60 * 5);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
		return iBinder;
	}

	public void sendBroadcast(String noticeKey, String aUnReadCount) {
		Intent intent = new Intent();
		intent.putExtra(noticeKey, noticeKey);
		intent.putExtra("unReadCount", aUnReadCount);
		intent.setAction(GlobalVars.ACTION_BROADCASTRECEIVEE);
		sendBroadcast(intent);
	}

	@Override
	public void onCreate() {
		Log.i("", "");
		Log.e("onStart", "服务正在启动，这里尝试同步联系人");
		LinkmanRequest.tryDoTongBu(getApplicationContext());
		RiChengRequest.tryInitAlerm(getApplicationContext());
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);

	}

}
